import numpy as np
import pandas as pd

# =====================================================
# 计算滇中新型城镇化与生态环境耦合度C和协调度D
# =====================================================
# 新型城镇化，生态环境
base_path = 'data/年鉴统计数据/地级市数据/处理结果-顾及时空统一性/'
file = base_path + '滇中-新型城镇化与生态环境-综合水平.xlsx'
df = pd.read_excel(file, index_col=0)

df_C = pd.DataFrame()
df_D = pd.DataFrame()
city_cv = df['新型城镇化']
city_en = df['生态环境']
# 计算乘积
product = city_cv * city_en
# 计算均值
mean = (city_cv + city_en) / 2
# 计算均值的平方
square_of_mean = mean * mean
# 计算耦合度
C = np.sqrt(product / square_of_mean)
df_C['耦合度'] = C
df['耦合度'] = C
# 计算T1
T1 = 1/2 * city_cv + 1/2 * city_en
# 计算协调度D
D1 = np.sqrt(C * T1)
df_D['类型1'] = D1
df['协调度'] = D1
# 计算T2
T2 = 1/3 * city_cv + 2/3 * city_en
# 计算协调度D2
D2 = np.sqrt(C * T2)
df_D['类型2'] = D2

# 计算T3
T3 = 2/3 * city_cv + 1/3 * city_en
# 计算协调度D3
D3 = np.sqrt(C * T3)
df_D['类型3'] = D3
print(df_D)

# 保存
df_C.to_excel(base_path + '滇中-新型城镇化与生态环境-耦合度.xlsx')
df_D.to_excel(base_path + '滇中-新型城镇化与生态环境-协调度.xlsx')
df.to_excel(base_path + '滇中-新型城镇化与生态环境-耦合协调度.xlsx')
